Sensor Fusion for Kinetis MCUs (ISSDK/KSDK version)
status.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2016, NXP Semiconductor
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without modification,
6  * are permitted provided that the following conditions are met:
7  *
8  * o Redistributions of source code must retain the above copyright notice, this list
9  * of conditions and the following disclaimer.
10  *
11  * o Redistributions in binary form must reproduce the above copyright notice, this
12  * list of conditions and the following disclaimer in the documentation and/or
13  * other materials provided with the distribution.
14  *
15  * o Neither the name of Freescale Semiconductor, Inc. nor the names of its
16  * contributors may be used to endorse or promote products derived from this
17  * software without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
21  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
23  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
26  * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */
30 
31 /*! \file status.c
32  \brief Application-specific status subsystem
33 
34  Applications may change how they choose to display status information.
35  The default implementation here uses LEDs on NXP Freedom boards.
36  You may swap out implementations as long as the "Required" methods and states
37  are retained.
38 */
39 
40 #include "board.h" // KSDK HAL
41 #include "fsl_port.h" // KSDK Port Module Interface
42 #include "sensor_fusion.h" // Sensor fusion structures and functions
43 #include "drivers.h" // Common driver header file
44 #include "status.h" // Header for this .c file
45 
46 // bit-field definitions
47 #define N 0x00 // No color
48 #define R 0x04 // Red LED
49 #define G 0x02 // Green LED
50 #define B 0x01 // Blue LED
51 
52 // set RGB LED as a function of bit-field values
53 void ssSetLeds(int8_t RGB)
54 {
55  if (RGB & R)
56  LED_RED_ON();
57  else
58  LED_RED_OFF();
59  if (RGB & G)
60  LED_GREEN_ON();
61  else
62  LED_GREEN_OFF();
63  if (RGB & B)
64  LED_BLUE_ON();
65  else
66  LED_BLUE_OFF();
67 }
68 
69 // Do an immediate status update
71 {
72  pStatus->status = status;
73  pStatus->next = status;
74 
75  uint8_t blink = false;
76  uint8_t RGB = N;
77 
78  // This is where we actually change the visual indicator
79  // We are not using the blue LED because it is not available on some
80  // board combinations.
81  switch (status)
82  {
83  case INITIALIZING: // solid GREEN
84  RGB = G;
85  break;
86  case NORMAL: // blinking GREEN
87  RGB = G;
88  blink = true;
89  break;
90  case LOWPOWER: // blinking YELLOW
91  RGB = R|G;
92  blink = true;
93  break;
94  case SOFT_FAULT: // solid RED (usually momentary)
95  case HARD_FAULT: // solid RED
96  RGB = R;
97  break;
98  default: // default = off;
99  RGB = N;
100  }
101 
102  if ((!blink) | (status != pStatus->previous))
103  {
104  ssSetLeds(RGB);
105  pStatus->toggle = true;
106  }
107  else
108  {
109  if (pStatus->toggle)
110  {
111  ssSetLeds(N);
112  }
113  else
114  {
115  ssSetLeds(RGB);
116  }
117 
118  pStatus->toggle = !pStatus->toggle;
119  }
120  while (status == HARD_FAULT) ; // Never return on hard fault
121  // while (status == SOFT_FAULT) ; // DEBUG ONLY Never return on soft fault
122 }
123 
124 // Unit test for status sub-system
125 void ssTest(StatusSubsystem *pStatus)
126 {
127  switch (pStatus->status)
128  {
129  case OFF:
130  ssSetStatusNow(pStatus, INITIALIZING);
131  break;
132  case INITIALIZING:
133  ssSetStatusNow(pStatus, LOWPOWER);
134  break;
135  case LOWPOWER:
136  ssSetStatusNow(pStatus, NORMAL);
137  break;
138  case NORMAL:
140  break;
141  case RECEIVING_WIRED:
143  break;
144  case RECEIVING_WIRELESS:
145  ssSetStatusNow(pStatus, SOFT_FAULT);
146  break;
147  case SOFT_FAULT:
148  ssSetStatusNow(pStatus, HARD_FAULT);
149  break;
150  case HARD_FAULT:
151  ssSetStatusNow(pStatus, OFF);
152  break;
153  }
154 }
155 
156 // undefine these just in case some other library needs them
157 #undef N
158 #undef R
159 #undef G
160 #undef B
161 
162 
163 // queue up a status change (which will take place at the next updateStatus)
165 {
166  pStatus->next = status;
167 }
168 
169 // promote any previously queued status update
171 {
172  pStatus->previous = pStatus->status;
173  ssSetStatusNow(pStatus, pStatus->next);
174 }
175 
176 // make an immediate update to the system status
178 {
179  pStatus->next = status;
180  ssUpdateStatus(pStatus);
181 }
182 
183 /// initializeStatusSubsystem() should be called once at startup to initialize the
184 /// data structure and to put hardware into the proper state for communicating status.
186 {
187  pStatus->set = ssSetStatus;
188  pStatus->queue = ssQueueStatus;
189  pStatus->update = ssUpdateStatus;
190  pStatus->test = ssTest;
191  pStatus->previous = OFF;
192  pStatus->set(pStatus, OFF);
193  pStatus->queue(pStatus, OFF);
194  pStatus->toggle = false;
195 
196  /* Un-gate the port clocks */
197  CLOCK_EnableClock(RED_LED.clockName);
198  CLOCK_EnableClock(GREEN_LED.clockName);
199  //CLOCK_EnableClock(BLUE_LED.clockName);
200 
201  /* Led pin mux Configuration */
202  PORT_SetPinMux(BOARD_LED_RED_GPIO_PORT, BOARD_LED_RED_GPIO_PIN,
203  kPORT_MuxAsGpio);
204  PORT_SetPinMux(BOARD_LED_GREEN_GPIO_PORT, BOARD_LED_GREEN_GPIO_PIN,
205  kPORT_MuxAsGpio);
206  //PORT_SetPinMux(BOARD_LED_BLUE_GPIO_PORT, BOARD_LED_BLUE_GPIO_PIN,
207  // kPORT_MuxAsGpio);
208 
209  /* set initial values */
210  LED_RED_INIT(LOGIC_LED_OFF);
211  LED_GREEN_INIT(LOGIC_LED_OFF);
212  //LED_BLUE_INIT(LOGIC_LED_OFF);
213 }
Initializing sensors and algorithms.
ssUpdateStatus_t * test
unit test which simply increments to next state
Definition: status.h:53
fusion_status_t status
Current status.
Definition: status.h:47
void ssSetStatusNow(StatusSubsystem *pStatus, fusion_status_t status)
Definition: status.c:70
Receiving commands over wireless interface (momentary)
void ssTest(StatusSubsystem *pStatus)
Definition: status.c:125
#define G
Definition: status.c:49
void initializeStatusSubsystem(StatusSubsystem *pStatus)
initializeStatusSubsystem() should be called once at startup to initialize the data structure and to ...
Definition: status.c:185
#define B
Definition: status.c:50
fusion_status_t previous
Previous status state - fusion_status_t is defined in sensor_fusion.h.
Definition: status.h:46
StatusSubsystem() provides an object-like interface for communicating status to the user...
Definition: status.h:44
These are the state definitions for the status subsystem.
void ssQueueStatus(StatusSubsystem *pStatus, fusion_status_t status)
Definition: status.c:164
uint8_t toggle
This implementation can change LED color and have either solid/toggle.
Definition: status.h:55
Recoverable FAULT = something went wrong, but we can keep going.
void ssSetLeds(int8_t RGB)
Definition: status.c:53
#define R
Definition: status.c:48
The sensor_fusion.h file implements the top level programming interface.
Receiving commands over wired interface (momentary)
Running in reduced power mode.
void ssUpdateStatus(StatusSubsystem *pStatus)
Definition: status.c:170
Provides function prototypes for driver level interfaces.
#define N
Definition: status.c:47
ssUpdateStatus_t * update
make pending status active/visible
Definition: status.h:52
Non-recoverable FAULT = something went very wrong.
Application-specific status subsystem.
void ssSetStatus(StatusSubsystem *pStatus, fusion_status_t status)
Definition: status.c:177
ssSetStatus_t * set
change status immediately - no delay
Definition: status.h:50
fusion_status_t
Application-specific serial communications system.
Operation is Nominal.
fusion_status_t next
Pending status change.
Definition: status.h:48
ssSetStatus_t * queue
queue status change for next regular interval
Definition: status.h:51